<?php

namespace app\common\model;

class ExamTime extends Base
{
    /**
     * 开启自动时间戳
     * @var bool
     */
    protected $autoWriteTimestamp = false;

    /**
     * 创建时间戳
     * @var string
     */
    protected $createTime = 'create_time';

    /**
     * 更新时间戳
     * @var string
     */
    protected $updateTime = 'update_time';

    /**
     * 按照专业id查询考试时间
     * @param $profession_id
     * @return ExamTime|array|mixed|\think\Model|null
     * @throws
     */
    public function matchExamTimeByProfessionId($profession_id)
    {
        $time = time();
        $matchList = [
            // 优先大于或者等于当前时间
            [
                'where' => [
                    ['profession_id', '=', $profession_id],
                    ['exam_type', '=', 1],
                    ['exam_time', '>=', $time],
                ],
                'order' => ['exam_time' => 'asc'],
            ],
            // 否则小于等于
            [
                'where' => [
                    ['profession_id', '=', $profession_id],
                    ['exam_type', '=', 1],
                    ['exam_time', '<=', $time],
                ],
                'order' => ['exam_time' => 'desc'],
            ],
        ];
        
        // 循环匹配
        foreach ($matchList as $match) {
            $examTimeRow = app(ExamTime::class)->where($match['where'])->cache(false)->order($match['order'])->find();
            if ($examTimeRow) {
                return $examTimeRow;
            }
        }
        return [];
    }
}